package zj.xinxin.business.strategy;

import com.alibaba.fastjson.JSON;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;
import zj.xinxin.common.enums.HttpResponseEnum;
import zj.xinxin.common.result.ResponseResult;

import javax.servlet.ServletException;
import java.io.IOException;

/**
 * @author junzhou
 * @date 2022/3/23 15:16
 * @since 1.8
 */
@Component
public class CustomExpiredSessionStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        event.getResponse().setContentType("application/json; charset=UTF-8");
        event.getResponse()
                .getWriter()
                .write(JSON.toJSONString(
                        ResponseResult.success(
                                HttpResponseEnum.USER_LOGIN_IN_OTHER_PLACE)));
    }
}
